
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import model.Film;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import persistence.FilmService;
import persistence.Installer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Matthy & Jelle
 */
public class TwitterSender {

  public static void main(String[] args) {


    try {
      // Create a ConnectionFactory
      ActiveMQConnectionFactory connectionFactory =
              new ActiveMQConnectionFactory("failover://(tcp://localhost:61616,tcp://localhost:61617)?randomize=false");
      // Create a Connection to ActiceMQ
      Connection connection = connectionFactory.createConnection();
      connection.start();
      // Create a Session that allows you to work with activeMQ
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      // Create the destination queue (or retrieve it, if it already exists)
      Destination destination = session.createQueue("TWITTER.QUEUE");
      // Create a MessageProducer for the Destination
      MessageProducer producer = session.createProducer(destination);
      producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

      for (int i = 0; i<10; i++) {
        Document doc = TwitterSender.generateTweet();
        XMLOutputter xmlOutput = new XMLOutputter();
        xmlOutput.setFormat(Format.getPrettyFormat());
        TextMessage message = session.createTextMessage(xmlOutput.outputString(doc));
        producer.send(message);
      }

      producer.close();

    } catch (JMSException ex) {
      System.out.println(ex.getMessage());
    }
    System.exit(1);


  }

  public static Document generateTweet() {
    Random generator = new Random();
    String characters = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ@#";
    FilmService filmService = new FilmService();
    Film film = filmService.getFilmById(generator.nextInt(300));
    Element bericht = new Element("bericht");
    Namespace XSI_NAMESPACE = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    bericht.addNamespaceDeclaration(XSI_NAMESPACE);
    bericht.setAttribute("noNamespaceSchemaLocation","bericht.xsd", XSI_NAMESPACE);

    Document doc = new Document(bericht);

    Element datum = new Element("datum");
    DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");
    datum.addContent(dateformat.format(new Date()));
    bericht.addContent(datum);

    Element netwerk = new Element("netwerk");
    netwerk.addContent("twitter");
    bericht.addContent(netwerk);

    Element filmElement = new Element("film");
    filmElement.addContent(film.getTitel());
    bericht.addContent(filmElement);

    Element tekst = new Element("tekst");
    tekst.addContent(Installer.generateString(generator, characters, 120));
    bericht.addContent(tekst);
    return doc;
  }
}
